#if FAKE
#r "paket:
nuget Fake.DotNet.Cli
nuget Fake.IO.FileSystem
nuget Fake.Core.Target //"
#endif
#load ".fake/build.fsx/intellisense.fsx"

(* Project Constants *)

let ProjectName = "Creator"

let OutputBuildDirectory = "./Builds"

(* End Project Constants *)
 
open System
open System.IO
open Fake.Core
open Fake.DotNet
open Fake.IO.Globbing.Operators
open Fake.Core.TargetOperators

 

// Creates an environment variable and logs its new value, then returns that value.
let SetAndLogEnvironmentVariable variableName variableValue =
    Environment.setEnvironVar variableName variableValue
    Trace.log (sprintf "%s: %s" variableName (Environment.environVar variableName))
    (Environment.environVar variableName)

// Get unity editor version from environment variable or use default.
let UnityEditorVersion =
    SetAndLogEnvironmentVariable
        "UNITY_EDITOR_VERSION"
        (Environment.environVarOrDefault "UNITY_EDITOR_VERSION" "2021.3.5f1c1")

// Path to Unity Installation on current system.
let UnityEditorPath =
    (sprintf
        (if Environment.isLinux then
             @"/opt/Unity/Hub/Editor/%s/Editor/Unity"
         elif Environment.isMacOS then
             @"/Applications/Unity/Hub/Editor/%s/Unity.app/Contents/MacOS/Unity"
         else
             @"D:\U3DEditor\%s\Editor\Unity.exe")
        UnityEditorVersion)

// Run the Unity commandline
let RunUnity args =
    let fullPath = Path.GetFullPath(".")

    let result =
        Shell.Exec(
            UnityEditorPath,
            (sprintf
                "-quit -batchmode -logFile \"%s\" -projectPath \"%s\" %s"
                (Path.Combine(fullPath, "Logs", "Build.log"))
                fullPath
                args)
        )

    if result <> 0 then
        failwithf "Unity exited with error %d. See Build.log for details" result

(* Target *)

Target.initEnvironment ()

Target.create "Clean" (fun _ -> !!OutputBuildDirectory |> Fake.IO.Shell.cleanDirs)

Target.create "Setup" (fun _ ->
    Trace.log (sprintf " --- Build started at %A --- " DateTime.Now)
    Trace.log " --- Setting up environmnet variables ---"

    // Setting up project name
    SetAndLogEnvironmentVariable "UNITY_PROJECT_NAME" ProjectName |> ignore
    SetAndLogEnvironmentVariable "UNITY_ANDROID_KEYSTORE" "D:\\UnityProjects\\UnityCi\\user.keystore"
    SetAndLogEnvironmentVariable "UNITY_ANDROID_KEYSTORE_PASSWORD" "123456"
    SetAndLogEnvironmentVariable "UNITY_ANDROID_KEY_ALIAS_PASSWORD" "123456"
    let versionNumber =
        SetAndLogEnvironmentVariable
            "UNITY_PROJECT_VERSION"
            (Environment.environVarOrDefault "UNITY_PROJECT_VERSION" "0.1.0")

    let buildNumber =
        SetAndLogEnvironmentVariable
            "UNITY_PROJECT_BUILD_NUMBER"
            (Environment.environVarOrDefault "UNITY_PROJECT_BUILD_NUMBER" "1")

    let outputDirectory =
        SetAndLogEnvironmentVariable
            "UNITY_OUTPUT_DIRECTORY"
            (Environment.environVarOrDefault "UNITY_OUTPUT_DIRECTORY" (Path.GetFullPath(OutputBuildDirectory)))

    Directory.CreateDirectory(outputDirectory) |> ignore

    Trace.log " --- Finished setting up environment variables --- ")

// Target.create "Windows64" (fun _ -> RunUnity "-executeMethod BuildScript.StandaloneWindows64")

Target.create "Android" (fun _ ->
    RunUnity "-executeMethod BuildScript.Android"
)

Target.create "All" ignore

"Clean" ==> "Setup" ==> "Android"

// "Clean" ==> "Setup" ==> "Windows64"

// "Clean" ==> "Setup" ==> "Windows64" ==> "All"

Target.runOrDefaultWithArguments "Android"

(* End Target *)